<?php

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

Class NewsController extends Controller {
	var $name = "News";
	var $helpers = array("Html");
	private function leftMenu($ext){
		$menu = "";
		$menu .= "<div id='cssmenu'>";
		$menu .=  "<ul>";
		$result = $this->News->query('select * from news_types where left_menu = 1');
		foreach ($result as $item)
		{
			if ($item["news_types"]["parent"] == null)
			{
				$result2 = $this->News->query('select * from news_types where parent = '.$item["news_types"]["id"]);
				if ($result2 == null){
					$menu .= "<li class='active'><a href='".$ext.$item["news_types"]["alias"]."'><span>".$item["news_types"]["name"]."</span></a></li>";
				}
				else {
					$menu .= "<li class='has-sub'><a href='".$ext.$item["news_types"]["alias"]."'><span>".$item["news_types"]["name"]."</span></a>";
					$menu .= "<ul>";
					foreach ($result2 as $item_child){
						$menu .= "<li><a href='".$ext.$item_child["news_types"]["alias"]."'><span>".$item_child["news_types"]["name"]."</span></a></li>";
					}
					$menu .= "</ul></li>";
				}
			}
		}
		$menu.= "</ul>";
		$menu.= "</div>";
		return $menu;
	}

	private function project(){
		$project  = "<div>";
		$result = $this->News->query("SELECT * FROM news where news_type_id = 100 limit 0,3 ");
		foreach ($result as $item) {
			$news_des = substr($item["news"]["description"],0,150);
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$project .= "<div class='project_pic'>"."<img class='project_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$project .= "<div  class='projects_des'>";
			$project .= "<div><p class='project_title'>".$item["news"]["name"]."</p></div>";
			$project .= "<div><p class='project_content'>".$news_des."</p></div>";
			$project .= "<div><p class='project_view'>Lượt xem: "." lần</p></div><br>";
			$project .= "</div>";
		}
		$project  .= "</div>";
		return $project;
	}

	
	public function newsContainer($id,$alias){
		$news_count = "";
		$result = $this->News->query("SELECT count(*) FROM news where news_type_id =".$id);
		foreach ($result as $item){
			$news_count = $item["0"]["count(*)"];
		}
		$page="";
		if ( !ISSET($_GET['page']) ){
			$page = 0 ;
		}
		else{
			$page = $_GET['page'] - 1 ;
		}
		$num_per_page = 5;
		$num_page = floor($news_count / $num_per_page) + 1;
		$newsContainer = "<div id='house'>";
		$query = "SELECT * FROM news where news_type_id =".$id." limit ".$page*$num_per_page.",".$num_per_page;
		$result = $this->News->query($query);
		foreach ($result as $item) {
			$news_des = substr($item["news"]["description"],0,250);
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$news_des .= "...";
			$newsContainer .= "<div class='news_pic'>"."<img class='news_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$newsContainer .= "<div  class='news_des'>";
			$newsContainer .= "<div><p class='news_title'>".$item["news"]["name"]."</p></div>";
			$newsContainer .= "<div><p class='news_date'>Ngày đăng: ".date("d/m/Y",strtotime($item["news"]["date"]))."</p></div><br>";
			$newsContainer .= "<div><p class='news_view'>Lượt xem: "." lần</p></div><br>";
			$newsContainer .= "<div><p class='news_content'>".$news_des."</p></div>";
			$newsContainer .= "<div><p class='news_more'><a class='news_more'href='../".$alias."/".$item["news"]["alias"]."' >>> Xem thêm</a></p></div>";
			$newsContainer .= "</div>";
			
		}
		$newsContainer .= "</div>";

		//Paging
		$paging = "<div class='pagination clearfix'>";
		$page++;
		if ($page == 1){
			if ($num_page == 1) {
				$paging .='<strong>1</strong>';
			}
			else {
				$paging .='<strong>1</strong>';
				$paging .='<a href="?page=2">2</a>';
				$paging .='<a href="?page='.($page+1).'">»</a>';
				$paging .='<a href="?page='.$num_page.'">Cuối</a>';
			}
		}
		else if ($page == $num_page && $num_page != 1){
			$paging .='<a href="?page=1">Đầu</a>';
			$paging .='<a href="?page='.($page-1).'">«</a>';
			$paging .='<a href="?page='.($page-1).'">'.($page-1).'</a>';
			$paging .='<strong>'.$page.'</strong>';
		}
		else {
			$paging .='<a href="?page=1">Đầu</a>';
			$paging .='<a href="?page='.($page-1).'">«</a>';
			$paging .='<a href="?page='.($page-1).'">'.($page-1).'</a>';
			$paging .='<strong>'.$page.'</strong>';
			$paging .='<a href="?page='.($page+1).'">'.($page+1).'</a>';
			$paging .='<a href="?page='.($page+1).'">»</a>';
			$paging .='<a href="?page='.$num_page.'">Cuối</a>';
		}
		$paging .= "</div>";

		return $newsContainer.$paging;
	}

	function curPageURL() {
		$pageURL = '';
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}

	public function  newsDetail(){
		$this->layout = "NewsDetail";
		$this->set('left_menu',$this->leftMenu("../"));
		$alias = $this->request->params['detail'];
		
		$newsDetail = "<div>";
		$result = $this->News->query("SELECT * FROM news WHERE alias = '".$alias."'");
		foreach ($result as $item){
			$newsDetail .= "<div class='newsDetail_pic'>"."<img class='newsDetail_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$newsDetail .= "<div  class='news_des'>";
			$newsDetail .= "<div><p class='newsDetail_title'>".$item["news"]["name"]."</p></div>";
			$newsDetail .= "<div><p class='newsDetail_date'>Ngày đăng: ".date("d/m/Y",strtotime($item["news"]["date"]))."</p></div><br>";
			$newsDetail .= "<div><p class='newsDetail_view'>Lượt xem: "." lần</p></div><br>";
			$newsDetail .= "<div><p class='newsDetail_des'>".$item["news"]["description"]."</p></div>";
			$newsDetail .= "<div><p class='newsDetail_content'><p class='newsDetail_content'>".$item["news"]["content"]."</p></div>";
			$newsDetail .= "</div>";
			$this->set('title_for_layout',$item["news"]["name"]);
		}
		$newsDetail .= "</div>";
		$this->set('content',$newsDetail);
		
	}

	public function  newsList(){
		$this->layout = "NewsList";
		$this->set('left_menu',$this->leftMenu("../"));
		$this->set('project',$this->project());
		$alias = $this->request->params['title'];
		$this->set('title_for_layout',$alias);
		$result = $this->News->query("SELECT id,name FROM news_types WHERE alias = '".$alias."'");
		foreach ($result as $item) {
			$type = $item["news_types"]["id"];
			$this->set('title_for_layout',$item["news_types"]["name"]);
		}
		$this->set('news_list',$this->newsContainer($type,$alias));
	}
	
	public $user = array();
	
	public function contact() {

		$this->layout = "Contact";
		$this->set('left_menu',$this->leftMenu("/"));
	}	
		
	public function send()
	{
		$this->autoRender=false;
		$name = $_GET["name"];
		$subject = $_GET["subject"];
		$email = $_GET["email"];
		$phone = $_GET["phone"];
		$messages = $_GET["messages"];
		$content = $email."--".$name."--".$phone."--".$messages;
	//if (!empty($this->request->data))
			//{
				$email = new CakeEmail();
				$email->config('gmail');
				
				$email->from('friendkenten@gmail.com')
					  ->to('cututu913@yahoo.com')
					  ->subject($subject);
				
				if($email->send($content))
				{
					$this->Session->setFlash(__('Email gửi thành công'),
						'defaut',array('class' => 'thành công'));
				}
			//}
	}

	
}


?>